package hrm.action.insa.licenceaction;

import hrm.action.Action;
import hrm.action.ActionForward;
import hrm.dto.insa.EmployeeDTO;
import hrm.dto.insa.LicenceDTO;
import hrm.dto.login.LoginDTO;
import hrm.serviceimple.EmployeeServiceImple;
import hrm.serviceimple.LicenceServiceImple;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InsaBeforeInsertLicence implements Action {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward = new ActionForward();
		EmployeeServiceImple employeeService = EmployeeServiceImple.getInstance();
		EmployeeDTO edto = new EmployeeDTO();
		edto.setId(getFindId(request));
		edto = employeeService.getEmployee(edto);

		LicenceDTO ldto = new LicenceDTO();
		ldto.setId(edto.getId());
		LicenceServiceImple licenceService = LicenceServiceImple.getInstance();
		List<LicenceDTO> licencelist = null;
		licencelist = licenceService.getAllLicence(ldto);

		request.setAttribute("employeeDTO", edto);
		request.setAttribute("licencelist", licencelist);

		actfoward.setUrl("insertLicence");	
		return actfoward;
	}

	private String getFindId(HttpServletRequest request){
		
		String findid = (String) request.getSession().getAttribute("findid");
		if(findid == null){
			findid = ((LoginDTO)request.getSession().getAttribute("login")).getId();
			request.setAttribute("findid", findid);
		}
		return findid; 
	}//
}
